package com.east.sng.entity.param;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Range;
import org.springframework.util.StringUtils;

import javax.validation.ValidationException;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * @author east
 * @date: 2021/12/31
 * @Description: stu receive param javabean
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class StuParam {

    @NotNull(message = "{param.id.notnull}")
    private Integer stuId;
    @NotBlank(message = "{stu.stuname.notblank}")
    private String stuName;
    @NotBlank(message = "{stu.stuparentname.notblank}")
    private String stuParentName;
    @NotBlank(message = "{stu.stuparentrelation.notblank}")
    @Pattern(regexp = "^[\\u4e00-\\u9fa5]{2,}\\/[\\u4e00-\\u9fa5]{2,}|其他$",message = "{stu.stuparentrelation.notblank}")
    private String stuParentRelation;
    private String stuOrtherParentRelation;
    @NotBlank(message = "{stu.stusex.notblank}")
    @Pattern(regexp = "^1|2$",message = "{stu.stusex.notblank}")
    private String stuSex;
    @NotBlank(message = "{stu.stuage.notnull}")
    @Range(min = 6,max = 16,message = "{stu.stuage.range}")
    private Integer stuAge;
    @NotBlank(message = "{stu.stuaddress.notblank}")
    private String stuAddress;
    @NotBlank(message = "{stu.stuparentphone.notblank}")
    @Pattern(regexp = "^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$",message = "{stu.stuparentphone.pattern}")
    private String stuParentPhone;
    @NotBlank(message = "{stu.coursename.notblank}")
    @Pattern(regexp = "^([1-9][0-9]*){1,2}$",message = "{stu.coursename.notblank}")
    private String courseName;
    private String remark;

    public boolean parseStuParentRelation(){
        if (!"其他".equals(this.stuParentRelation)) {
            int index;String stuParentRelation;
            String[] arr = this.stuParentRelation.split("/");
            if ((index=Integer.parseInt(this.stuSex) - 1) < arr.length){
                if ((stuParentRelation = arr[index]) != null){
                    this.stuParentRelation = stuParentRelation;
                    return true;
                }
            }
            throw new ValidationException("stuParentRelation:学员与监护人关系选项有误");
        } else {
            if (!StringUtils.isEmpty(this.stuOrtherParentRelation) && this.stuOrtherParentRelation.matches("^[\\u4e00-\\u9fa5]{2,}$")) {
                this.stuParentRelation = this.stuOrtherParentRelation;
                return true;
            }
            throw new ValidationException("stuOrtherParentRelation:学员与监护人关系输入有误");
        }
    }

}